<template>
	<view class="">
		<input class="input-r  sanjiao-right" type="text" :placeholder="placeholder"
			placeholder-class="placeholder-class" style="text-align: right;" v-model="realName" @click="open = true"
			@input="$u.debounce(emitChange, 700)" />
		<view class="selbox" v-if="open && list.length>0">
			<scroll-view scroll-y="true" class="scroll-Y">
				<view class="scroll-view-item">
					<view class="uni-popper__arrow"></view>
					<view v-for="item in list" :key="item[keyId]">
						<view class="selItem" @click="uchange(item)">{{item[keyName]}}</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class='mask' v-if="open&&list.length>0" @click="open = false"></view>
	</view>
</template>

<script>
	export default {
		props: {
			list: {
				type: Array,
				default: () => [{
						value: 0,
						text: "篮球"
					},
					{
						value: 1,
						text: "足球"
					},
					{
						value: 2,
						text: "游泳"
					}
				]
			},
			keyName: {
				type: String,
				default: 'name'
			},
			keyId: {
				type: [String, Number],
				default: 'id'
			},
			value: {
				type: [String, Number],
				default: ''
			},
			placeholder: {
				type: String,
				default: '请选择'
			},
			name: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				realName: this.name,
				open: false
			}
		},
		methods: {
			uchange(item) {
				this.realName = item[this.keyName];
				this.$emit('input', item[this.keyId]);
				this.$emit('uchange', item)
				this.open = false
			},
			emitChange(val) {
				this.$emit('inChange', this.realName);
			},
			initVal() {
				this.list.forEach(item => {
					if (item[this.keyId] == this.value) {
						this.realName = item[this.keyName]
					}
				})
			}
		},
		watch: {
			name() {
				console.log("变化", this.name)
				this.realName = this.name;
			},
			value(newVal) {
				this.$nextTick(() => {
					this.initVal();
				})

			}
		}
	}
</script>

<style>
	.selbox {
		width: 80%;
		max-height: 400rpx;
		background-color: #fff;
		position: absolute;
		z-index: 99;
		font-size: 26rpx;
		padding: 20rpx;
		right: 20rpx;
		box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
		border: 1px solid #ebeef5;
		border-radius: 6px;
		margin-top: 30rpx;
	}

	.selItem {
		height: 80rpx;
		line-height: 80rpx;
		font-size: 26rpx;
		color: #000;
		padding: 0 20rpx;
		text-align: left;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.selItem:hover {
		background: #f7f7f7;
		border-radius: 15rpx;
	}

	.uni-popper__arrow {
		filter: drop-shadow(0 2px 12px rgba(0, 0, 0, .03));
		top: -6px;
		left: 10%;
		margin-right: 3px;
		border-top-width: 0;
		border-bottom-color: #ebeef5;
		/* background: #000; */
		position: absolute;
		display: block;
		width: 0;
		height: 0;
		border-color: transparent;
		border-style: solid;
		border-width: 6px;
	}

	.mask {
		position: fixed;
		top: 0;
		right: 0;
		left: 0;
		bottom: 0;
		z-index: 10;
		opacity: 0.1
	}

	.scroll-view-item {
		max-height: 360rpx;
	}
</style>